Index: lib/db/install.xml =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/install.xml,v retrieving revision 1.135.2.16 diff -u -r1.135.2.16 install.xml --- lib/db/install.xml 8 Sep 2009 09:06:15 -0000 1.135.2.16 +++ lib/db/install.xml 14 Dec 2009 12:09:37 -0000 @@ -1,5 +1,5 @@ - @@ -1652,7 +1652,7 @@ - +
@@ -1674,6 +1674,25 @@
+ + + + + + + + + + + + + + + + + + +
Index: lib/db/upgrade.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v retrieving revision 1.154.2.67 diff -u -r1.154.2.67 upgrade.php --- lib/db/upgrade.php 26 Nov 2009 06:48:43 -0000 1.154.2.67 +++ lib/db/upgrade.php 14 Dec 2009 12:06:04 -0000 @@ -3291,6 +3291,34 @@ upgrade_main_savepoint($result, 2007101563.03); } + if ($result && $oldversion < 2007101563.04) { + // add the new "friends" table + + /// Define table friend to be created + $table = new XMLDBTable('friend'); + + /// Adding fields to table friend + $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->addFieldInfo('userid1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('userid2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('status', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->addFieldInfo('created', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->addFieldInfo('message', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + + /// Adding keys to table friend + $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->addKeyInfo('userid1', XMLDB_KEY_FOREIGN, array('userid1'), 'user', array('id')); + $table->addKeyInfo('userid2', XMLDB_KEY_FOREIGN, array('userid2'), 'user', array('id')); + + /// Adding indexes to table friend + $table->addIndexInfo('created', XMLDB_INDEX_NOTUNIQUE, array('created')); + $table->addIndexInfo('status', XMLDB_INDEX_NOTUNIQUE, array('status')); + + /// Launch create table for friend + $result = $result && create_table($table); + + upgrade_main_savepoint($result, 2007101563.04); + } return $result; } Index: user/tabs.php =================================================================== RCS file: /cvsroot/moodle/moodle/user/tabs.php,v retrieving revision 1.43.2.8 diff -u -r1.43.2.8 tabs.php --- user/tabs.php 19 Dec 2008 01:44:48 -0000 1.43.2.8 +++ user/tabs.php 14 Dec 2009 11:53:36 -0000 @@ -268,6 +268,12 @@ } } } + +// Friends tab + if (!empty($user)) { // and (!empty($CFG->enablefriends)) + $toprow[] = new tabobject('friends', $CFG->wwwroot.'/friends/view.php?userid='.$user->id.'&courseid='.$course->id, get_string('friends', 'friends')); + } + /// Add second row to display if there is one if (!empty($secondrow)) { Index: user/view.php =================================================================== RCS file: /cvsroot/moodle/moodle/user/view.php,v retrieving revision 1.168.2.28 diff -u -r1.168.2.28 view.php --- user/view.php 2 Oct 2009 06:28:06 -0000 1.168.2.28 +++ user/view.php 14 Dec 2009 11:54:42 -0000 @@ -532,6 +532,18 @@ echo ""; echo ""; } + + // Add as friend button + if (!empty($USER->id) and ($USER->id != $user->id)) { + echo "
"; + echo "
"; + echo "id\" />"; + echo "id\" />"; + echo ""; + echo "
"; + echo "
"; + } + echo "\n"; if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $USER->id == $user->id) { // Show user object --- friends/add.php +++ friends/add.php @@ -0,0 +1,126 @@ +enablefriends)) { print_error('friendsaredisabled', 'friends'); } + +$userid = optional_param('userid', 0, PARAM_INT); +$courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user tabs and course tracking + +$friendsstring = get_string('addfriend', 'friends'); +$navlinks = array(); + +if (!empty($userid)) { + $user = get_record('user', 'id', $userid); + $course = get_record('course', 'id', $courseid); + + $a = fullname($user); + $title = get_string('requestfriendship', 'friends', $a); + + $navlinks[] = array('name' => fullname($user), + 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", + 'type' => 'misc'); + $navlinks[] = array('name' => $title, 'link' => null, 'type' => 'misc'); + $navigation = build_navigation($navlinks); + print_header("$course->shortname: $friendsstring", $course->fullname, + $navigation, "", "", true, " ", navmenu($course)); + + $currenttab = 'friends'; + $showroles = 1; + include('../user/tabs.php'); +} +else { + $title = get_string('friends', 'friends'); + $navlinks[] = array('name' => $title, 'link' => null, 'type' => 'misc'); + $navigation = build_navigation($navlinks); + print_header_simple(get_string('friends', 'friends'), '', $navigation); +} + +$friendform = new friends_add_form(); + +// If new data has been sent, add friend request +if ($newfriend = $friendform->get_data()) { + + friends_add($newfriend); + + friends_print_message(get_string('friendshiprequested', 'friends'), + $CFG->wwwroot.'/user/view.php?id='.$newfriend->userid2.'&course='.$newfriend->courseid, + get_string('return', 'friends')); + + print_footer(); + return; +} +else { + $friends = friends_get_friends($USER->id, $user->id); + + if (!empty($friends)) { + + if (friends_exist_friend_status($friends, FRIENDS_STATUS_FRIEND)) { + + // Friendship already exist + + friends_print_message(get_string('alreadyfriends', 'friends')); + print_footer(); + return; + + } + else { + + $friend = friends_get_friend_by_status($friends, FRIENDS_STATUS_NEW); + + if (!empty($friend)) { + + // Friendship request already exist + + if ($friend->userid1 == $user->id) { + + // The other user requested -> Confirm friendship + + $friend->status = FRIENDS_STATUS_FRIEND; + friends_update($friend); + + friends_print_message(get_string('alreadyrequested', 'friends') . '
' . get_string('friendshipconfirmed', 'friends')); + + } else { + + // I requested -> Warn + + friends_print_message(get_string('alreadyrequested', 'friends') . ' ' . get_string('pleasewaitconfirmation', 'friends')); + } + + print_footer(); + return; + } + } + } + + $newfriend = friends_create($USER->id, $user->id); + $newfriend->courseid = $courseid; + + $friendform->set_data($newfriend); + + print_heading($title, '', 2); + + $friendform->display(); + + print_footer($course); +} + + + + +?> --- friends/add_form.php +++ friends/add_form.php @@ -0,0 +1,44 @@ +dirroot.'/lib/formslib.php'); + +class friends_add_form extends moodleform { + + function definition () { + + $mform =& $this->_form; + + $mform->addElement('header', 'friends', get_string('addfriend','friends')); + + $mform->addElement('hidden', 'userid1'); + $mform->setType('userid1', PARAM_INT); + + $mform->addElement('hidden', 'userid2'); + $mform->setType('userid2', PARAM_INT); + + $mform->addElement('hidden', 'courseid'); + $mform->setType('courseid', PARAM_INT); + + $mform->addElement('hidden', 'created'); + $mform->setType('created', PARAM_INT); + + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + + $mform->addElement('textarea', 'message', get_string('introductionmessage', 'friends'), array('rows'=>5, 'cols'=>50)); + + $this->add_action_buttons(false, get_string('addfriend', 'friends')); + + } + +} + +?> --- friends/confirm.php +++ friends/confirm.php @@ -0,0 +1,58 @@ +enablefriends)) { print_error('friendsaredisabled', 'friends'); } + +$userid = optional_param('userid', 0, PARAM_INT); +$courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user tabs and course tracking + +$confirm = optional_param('confirm', 99, PARAM_INT); + +if (empty($userid) || empty($courseid) || $confirm == 99) { + print_error('Wrong parameters'); +} + +$friends = friends_get_friends($USER->id, $userid); +$friend = friends_get_friend_by_status($friends, FRIENDS_STATUS_NEW); + +if (empty($friend)) { + print_error('Friend record not found'); +} +else { + if ($friend->userid1 != $userid) { + print_error('Wrong friendship status'); + } + else { + // The other user requested -> Confirm or deny friendship + + if ($confirm == 1) { + // Confirm + $friend->status = FRIENDS_STATUS_FRIEND; + friends_update($friend); + } + else { + // Deny (delete record) + friends_delete($friend); + } + } + + redirect($CFG->wwwroot.'/friends/view.php?courseid='.$courseid.'&userid='.$USER->id); +} + + +?> --- friends/delete.php +++ friends/delete.php @@ -0,0 +1,106 @@ +enablefriends)) { print_error('friendsaredisabled', 'friends'); } + +$userid = optional_param('userid', 0, PARAM_INT); +$messageid = optional_param('messageid', 0, PARAM_INT); +$courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user tabs and course tracking + +$confirm = optional_param('confirm', 0, PARAM_INT); // if == 1 delete, else ask + +if (!empty($userid)) { + $friends = friends_get_friends($USER->id, $userid); + $friend = friends_get_friend_by_status($friends, FRIENDS_STATUS_FRIEND); + $deletingfriend = true; +} +else if (!empty($messageid)) { + $friend = get_record('friend', 'id', $messageid); + $deletingfriend = false; +} + +if (empty($friend)) { + print_error('Friend record not found'); +} + +if ($deletingfriend) { + $whereiam = $USER->id; // home profile +} +else { + $whereiam = $friend->userid2; // recipient profile +} + +if ($confirm == 1) { + + // confirmed -> really delete + + friends_delete($friend); + redirect($CFG->wwwroot.'/friends/view.php?courseid='.$courseid.'&userid='.$whereiam); + +} +else { + + // not confirmed -> ask first + + $targetuserid = $userid; + $userid = $whereiam; + $user = get_record('user', 'id', $userid); + $course = get_record('course', 'id', $courseid); + + if ($deletingfriend) { + $title = get_string('delete', 'friends'); + } + else { // deleting message + $title = get_string('deletemsg', 'friends'); + } + $friendsstring = get_string('friends', 'friends'); + $navlinks[] = array('name' => fullname($user), + 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&course=$courseid", + 'type' => 'misc'); + $navlinks[] = array('name' => $title, 'link' => null, 'type' => 'misc'); + $navigation = build_navigation($navlinks); + print_header("$course->shortname: $friendsstring", $course->fullname, + $navigation, "", "", true, " ", navmenu($course)); + + $currenttab = 'friends'; + $showroles = 1; + include('../user/tabs.php'); + + if ($deletingfriend) { + $targetuser = get_record('user', 'id', $targetuserid); + $a = fullname($targetuser); + $stringareyousure = get_string('deleteareyousure', 'friends', $a); + $deletelink = 'userid='.$targetuserid; + $deleteconfirm = get_string('deleteconfirm', 'friends'); + } + else { // deleting message + $targetuser = get_record('user', 'id', $targetuserid); + $a = fullname($targetuser); + $stringareyousure = get_string('deletemsgareyousure', 'friends', $a); + $deletelink = 'messageid='.$messageid; + $deleteconfirm = get_string('deletemsgconfirm', 'friends'); + } + + friends_print_message($stringareyousure, + 'delete.php?'.$deletelink.'&courseid='.$courseid.'&confirm=1', + $deleteconfirm); + + print_footer(); + return; +} + +?> --- friends/lib.php +++ friends/lib.php @@ -0,0 +1,165 @@ +prefix}friend fr INNER JOIN {$CFG->prefix}user usr ". + "ON ((usr.id = fr.userid1 AND fr.userid2 = {$userid}) ". + "OR (usr.id = fr.userid2 AND fr.userid1 = {$userid})) ". + "WHERE fr.status = ".$status; + + return get_records_sql($query, $limitfrom , $limitnum); +} + +/** + * get all messages of the user in an array (only userid1 will be matched) + * + * @param int $userid User we want to find the friends + * @param int $status Status of friendship + * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). + * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). + * @return array array of friends, or empty + */ +function friends_get_user_messages($userid, $limitfrom='', $limitnum='') { + global $CFG; + + $query = "SELECT fr.id friendid, usr.id userid, usr.picture, ". + "usr.firstname, usr.lastname, fr.message, fr.userid1, fr.userid2, fr.created ". + "FROM {$CFG->prefix}friend fr INNER JOIN {$CFG->prefix}user usr ". + "ON (fr.userid1=usr.id) WHERE ((fr.userid2={$userid}) AND ". + "(fr.status = ".FRIENDS_STATUS_MESSAGE.")) ". + "ORDER BY fr.created DESC"; + + return get_records_sql($query, $limitfrom, $limitnum); +} + +/** + * get all friend records between two users, return an array + * + * @param int $userid1 + * @param int $userid2 + * @return array array of friends, or empty + */ +function friends_get_friends($userid1, $userid2) { + global $CFG; + + $query = "SELECT fr.id, fr.* ". + "FROM {$CFG->prefix}friend fr ". + "WHERE (fr.userid1 = {$userid1} AND fr.userid2 = {$userid2}) ". + "OR (fr.userid1 = {$userid2} AND fr.userid2 = {$userid1})"; + + return get_records_sql($query); +} + +/** + * scan an array of friends trying to find the status + * + * @param array $friends array of friends being searched + * @param int $status status to be searched + * @return bool true if exist + */ +function friends_exist_friend_status($friends, $status) { + $friend = friends_get_friend_by_status($friends, $status); + return (!empty($friend)); +} + +/** + * scan an array of friends trying to find the status + * + * @param array $friends array of friends being searched + * @param int $status status to be searched + * @return object friend found or empty + */ +function friends_get_friend_by_status($friends, $status) { + if (!empty($friends)) { + foreach ($friends as $friend) { + if ($friend->status == $status) { + return $friend; + } + } + } + + return null; +} + +/** + * create a friend relationship between userid1 and userid2 + * + * @param int $userid1 User requesting friendship (logged user) + * @param int $userid2 User with friendship being requested + * @param int $status Record type (0 = new, 1 = friend, 2 = message) + * @return object Created object + */ +function friends_create($userid1, $userid2, $status=0, $message='') { + $friend_object = new StdClass; + $friend_object->userid1 = $userid1; + $friend_object->userid2 = $userid2; + $friend_object->status = $status; + $friend_object->message = $message; + $friend_object->created = time(); + + return $friend_object; +} + +/** + * create a message between userid1 and userid2 + * + * @param int $userid1 User requesting friendship (logged user) + * @param int $userid2 User with friendship being requested + * @return object Created object + */ +function friends_create_message($userid1, $userid2) { + return friends_create($userid1, $userid2, FRIENDS_STATUS_MESSAGE); +} + +/** + * add a friend_object to database table friend + * + * @param object $friend_object Object being inserted + * @return int id of created record + */ +function friends_add($friend_object) { + return insert_record('friend', $friend_object); +} + +/** + * update friend_object on database + * + * @param object $friend_object Object being updated + * @return int id of record + */ +function friends_update($friend_object) { + return update_record('friend', $friend_object); +} + +/** + * delete friend_object on database + * + * @param object $friend_object Object being deleted + * @return bool true if deleted + */ +function friends_delete($friend_object) { + return delete_records('friend', 'id', $friend_object->id); +} --- friends/locallib.php +++ friends/locallib.php @@ -0,0 +1,389 @@ +id) { + return $CFG->wwwroot .'/user/view.php?id='.$userid.$courselink; + } + else { + return ''; + } +} + + /** + * Prints an user picture + * + * @param object $user user object (contains the following fields: userid, picture, firstname, lastname) + * @param int $courseid to assemble links correctly + * @param $return if true return html string + */ +function friends_print_user_picture($user, $courseid, $return=false) { + global $CFG, $USER; + + $usercontext = get_context_instance(CONTEXT_USER, $user->userid); + $profilelink = friends_get_profile_link($user->userid, null); + $output = ''; + $fullname = fullname($user); + $alt = ''; + + if (!empty($profilelink)) { + $output .= ''; + $alt = $fullname; + } + + //print user image - if image is only content of link it needs ALT text! + if ($user->picture) { + $output .= ''. $alt .''; + } else { + $output .= ''. $alt .''; + } + + $output .= '
'; + + if (!empty($profilelink)) { + $output .= '
'; + } + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints an individual user box + * + * @param $user user object (contains the following fields: userid, picture, firstname, lastname) + * @param int $courseid to assemble links correctly + * @param bool $looking_at_my_profile + * @param $return if true return html string + */ +function friends_print_user_box($user, $courseid, $looking_at_my_profile, $return=false) { + + $output = print_box_start('user-box', 'user'. $user->userid, true); + + $output .= friends_print_user_picture($user, $courseid, true); + + $textlib = textlib_get_instance(); + + $fullname = fullname($user); + + //truncate name if it's too big + if ($textlib->strlen($fullname) > 26) { + $fullname = $textlib->substr($fullname, 0, 26) .'...'; + } + + $output .= ''. $fullname .''; + + if ($looking_at_my_profile) { + $output .= '
'; + $output .= friends_print_image_link("/friends/delete.php?userid=".$user->userid."&courseid=".$courseid, + "/pix/t/delete.gif", get_string("deletefriendtitle", "friends")); + $output .= friends_print_image_link("/friends/view.php?userid=".$user->userid."&courseid=".$courseid, + "/pix/t/email.gif", get_string("sendmessagetitle", "friends")); + } + + $output .= print_box_end(true); + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Returns an image link + * + * @param string $link + * @param string $image + * @param string $description for alt and title + */ +function friends_print_image_link($link, $image, $description) { + global $CFG; + + return ' '. + ''.$description.''; +} + +/** + * Prints a list of users + * + * @param array $userlist an array of user objects + * @param int $courseid to assemble links correctly + * @param bool $looking_at_my_profile + * @param $return if true return html string + */ +function friends_print_user_list($userlist, $courseid, $looking_at_my_profile, $return=false) { + $output = null; + + if (!empty($userlist)) { + $output = '
    '; + + foreach ($userlist as $user){ + $output .= '
  • '. friends_print_user_box($user, $courseid, $looking_at_my_profile, true) ."
  • \n"; + } + $output .= "
\n"; + } + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints a friends table + * + * @param int $userid + * @param int $courseid to assemble links correctly + * @param bool $looking_at_my_profile + * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set). + * @param int $limitnum prints this many users (optional, required if $limitfrom is set). + * @param $return if true return html string + */ +function friends_print_friends_table($userid, $courseid, $looking_at_my_profile, $limitfrom='' , $limitnum='', $return=false) { + + //List of friends of this user + $userlist = friends_get_user_friends($userid, FRIENDS_STATUS_FRIEND, $limitfrom, $limitnum); + + $output = friends_print_user_list($userlist, $courseid, $looking_at_my_profile, true); + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints a list of requests to be friend of userid + * + * @param int $userid + * @param int $courseid necessary to assemble the links + * @param $return if true return html string + */ +function friends_print_friends_requests($userid, $courseid, $return=false) { + global $CFG; + + //List of friends of this user + $requestlist = friends_get_user_friends($userid, FRIENDS_STATUS_NEW); + + $count = 0; + $output = ''; + + if (!empty($requestlist)) { + + foreach ($requestlist as $user){ + if ($user->userid2 == $userid) { + + // Only cases that $userid have been requested (=userid2), not requested (=userid1) + + $output .= '\n"; + + $a = fullname($user); + + $output .= ''; + + $count++; + } + } + } + + $output .= "
'. friends_print_user_picture($user, $courseid, true) ."'.get_string('wantstobefriend', 'friends', $a).'

'; + $output .= ''.stripslashes($user->message).'

'; + $output .= get_string('addfriendqm', 'friends'); + + $output .= "
"; + $output .= ""; + $output .= ""; + $output .= ""; + $output .= ""; + $output .= "
"; + + $output .= "
"; + $output .= ""; + $output .= ""; + $output .= ""; + $output .= ""; + $output .= "
"; + + $output .= '
\n"; + + if ($count == 0) { + $output = null; + } + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints a list of messages sent to the user + * + * @param int $userid + * @param int $courseid necessary to assemble the links + * @param bool $looking_at_my_profile + * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set). + * @param int $limitnum prints this many users (optional, required if $limitfrom is set). + * @param $return if true return html string + */ +function friends_print_friend_messages($userid, $courseid, $looking_at_my_profile, $limitfrom='', $limitnum='', $return=false) { + global $CFG, $USER; + + $messagelist = friends_get_user_messages($userid, $limitfrom, $limitnum); + + $count = 0; + $output = ''; + + if (!empty($messagelist)) { + + foreach ($messagelist as $message){ + $output .= '\n"; + + // Print sender name + + $a = fullname($message); + + $profilelink = friends_get_profile_link($message->userid, null); + if (!empty($profilelink)) { + $a = ''.$a.''; + } + + $output .= ''; + + $count++; + } + } + + $output .= "
'. friends_print_user_picture($message, $courseid, true) ."'.get_string('userwrote', 'friends', $a); + + // Print message time + + $datestring = null; + $datestring->year = 'y'; $datestring->years = 'y'; + $datestring->day = 'd'; $datestring->days = 'd'; + $datestring->hour = 'h'; $datestring->hours = 'h'; + $datestring->min = 'm'; $datestring->mins = 'm'; + $datestring->sec = 's'; $datestring->secs = 's'; + + $a = str_replace(' ', '', format_time(time() - $message->created, $datestring)); + $output .= ' ('.get_string('timeago', 'friends', $a).' / '; + $output .= userdate($message->created, get_string("strftimerecent")).') '; + + if ($looking_at_my_profile or ($message->userid == $USER->id)) { + + // Show message delete link if im looking at my profile or if this message was written by me + + $output .= friends_print_image_link("/friends/delete.php?messageid=".$message->friendid."&courseid=".$courseid, + "/pix/t/delete.gif", get_string("deletemessagetitle", "friends")); + } + + $output .= '

'; + + + $output .= ''.stripslashes($message->message).''; + + $output .= '
\n"; + + if ($count == 0) { + $output = null; + } + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints a warning message centralized, with an optional link below. + * + * @param string $message + * @param string $linkurl + * @param string $linktext + * @param $return if true return html string + */ +function friends_print_message($message, $linkurl=null, $linktext=null, $return=false) { + $output = '
'.$message; + if (!empty($linktext) && !empty($linkurl)) { + $output .= '
'.$linktext.''; + } + $output .= '

'; + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints the beginning of a box, with a title + * + * @param string $title + * @param $return if true return html string + */ +function friends_begin_box($title, $return=false) { + $output = '
'.$title.''; + + if ($return) { + return $output; + } + else { + echo $output; + } +} + +/** + * Prints the end of a box + * + * @param $return if true return html string + */ +function friends_end_box($return=false) { + $output = '
'; + + if ($return) { + return $output; + } + else { + echo $output; + } +} --- friends/message_form.php +++ friends/message_form.php @@ -0,0 +1,46 @@ +dirroot.'/lib/formslib.php'); + +class friends_message_form extends moodleform { + + function definition () { + + $mform =& $this->_form; + + $mform->addElement('hidden', 'userid1'); + $mform->setType('userid1', PARAM_INT); + + $mform->addElement('hidden', 'userid2'); + $mform->setType('userid2', PARAM_INT); + + $mform->addElement('hidden', 'courseid'); + $mform->setType('courseid', PARAM_INT); + + $mform->addElement('hidden', 'created'); + $mform->setType('created', PARAM_INT); + + $mform->addElement('hidden', 'status'); + $mform->setType('status', PARAM_INT); + + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + + $mform->addElement('textarea', 'message', get_string('messagedescription', 'friends'), array('rows'=>3, 'cols'=>50)); + $mform->addRule('message', null, 'required', null, 'client'); + + $this->add_action_buttons(false, get_string('sendmessagebutton', 'friends')); + + } + +} + +?> --- friends/view.php +++ friends/view.php @@ -0,0 +1,182 @@ +enablefriends)) { print_error('friendsaredisabled', 'friends'); } + +$userid = optional_param('userid', 0, PARAM_INT); +$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs and course tracking +$message = optional_param('message', 0, PARAM_INT); // if == 1 warn that the message have been sent + +$messageform = new friends_message_form(); + +// If new data has been sent, add the message record +if ($messagetosend = $messageform->get_data()) { + friends_add($messagetosend); + redirect($CFG->wwwroot.'/friends/view.php?courseid='.$messagetosend->courseid.'&userid='.$messagetosend->userid2.'&message=1'); +} + +if (empty($userid) || empty($courseid)) { + redirect($CFG->wwwroot.'/'); +} + +$user = get_record('user', 'id', $userid); +$course = get_record('course', 'id', $courseid); + +$a = fullname($user); +$title = get_string('friendsof', 'friends', $a); +$friendsstring = get_string('friends', 'friends'); +$navlinks = array(); +$navlinks[] = array('name' => fullname($user), + 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", + 'type' => 'misc'); +$navlinks[] = array('name' => $title, 'link' => null, 'type' => 'misc'); + +$navigation = build_navigation($navlinks); + +print_header("$course->shortname: $friendsstring", $course->fullname, + $navigation, "", "", true, " ", navmenu($course)); + +$currenttab = 'friends'; +$showroles = 1; +include('../user/tabs.php'); + +//echo ''; + +echo '
'; + +$looking_at_my_profile = ($USER->id == $userid); + +if ($looking_at_my_profile) { + + // Show friend requests + + $result = friends_print_friends_requests($userid, $courseid, true); + + if (!empty($result)) { + friends_begin_box(get_string('friendrequests', 'friends')); + echo $result; + friends_end_box(); + } + +} +else { + + $friends = friends_get_friends($USER->id, $userid); + if (empty($friends)) { + + // No relationship yet: offer to add + + friends_begin_box(get_string('addfriend', 'friends')); + friends_print_message(get_string('notyetfriend', 'friends'), + 'add.php?userid='.$userid.'&courseid='.$courseid, + get_string('clicktoadd', 'friends')); + friends_end_box(); + } + else { + + $friend = friends_get_friend_by_status($friends, FRIENDS_STATUS_NEW); + if (!empty($friend)) { + + // Friendship request pending + + friends_begin_box(get_string('addfriend', 'friends')); + friends_print_message(get_string('pendingrequest', 'friends')); + friends_end_box(); + } + + $friend = friends_get_friend_by_status($friends, FRIENDS_STATUS_FRIEND); + if (!empty($friend)) { + + // User is viewing a friend's page. Show message form + + friends_begin_box(get_string('sendmessage', 'friends')); + + if ($message == 1) { + friends_print_message(''.get_string('messagesent', 'friends').''); + } + + $newmessage = friends_create_message($USER->id, $userid); + $newmessage->courseid = $courseid; + + $messageform->set_data($newmessage); + + // Prevent rendering the fieldset tag to make it more compact + $GLOBALS['_HTML_QuickForm_default_renderer']->setOpenHiddenFieldsetTemplate(''); + $GLOBALS['_HTML_QuickForm_default_renderer']->setCloseFieldsetTemplate(''); + + // Dont show required information + $GLOBALS['_HTML_QuickForm_default_renderer']->setRequiredNoteTemplate(''); + $messageform->_form->_reqHTML = ''; + + $messageform->display(); + + friends_end_box(); + } + } +} + +// List of friends + +friends_begin_box(get_string('listoffriends', 'friends')); + +$result = friends_print_friends_table($userid, $courseid, $looking_at_my_profile, '', '', true); // TODO: pagination + + +if (!empty($result)) { + echo $result; +} +else { + + // No friends found + + if ($looking_at_my_profile) { + friends_print_message(get_string('ihavenofriends', 'friends')); + } + else { + friends_print_message(get_string('hehavenofriends', 'friends')); + } +} + +friends_end_box(); + +// List of messages + +friends_begin_box(get_string('messages', 'friends')); + +$result = friends_print_friend_messages($userid, $courseid, $looking_at_my_profile, '', '', true); // TODO: pagination + +if (!empty($result)) { + echo $result; +} +else { + // No messages found + friends_print_message(get_string('nomessages', 'friends')); +} + +friends_end_box(); + +echo '
'; + +print_footer($course); + + +?> --- lang/en_utf8/friends.php +++ lang/en_utf8/friends.php @@ -0,0 +1,45 @@ +$a from your list of friends?'; +$string['deleteconfirm'] = 'Yes, remove the user'; +$string['deletefriendtitle'] = 'Remove this user from your list of friends'; +$string['deletemessagetitle'] = 'Remove this message'; +$string['deletemsg'] = 'Remove message'; +$string['deletemsgareyousure'] = 'Are you sure you want to remove this message?'; +$string['deletemsgconfirm'] = 'Yes, delete the message'; +$string['friendrequests'] = 'Friend requests'; +$string['friends'] = 'Friends'; +$string['friendsaredisabled'] = 'Friends are disabled'; +$string['friendshipconfirmed'] = 'Friendship successfully confirmed.'; +$string['friendshiprequested'] = 'Friendship successfully requested.'; +$string['friendsof'] = 'Friends of $a'; +$string['hehavenofriends'] = 'This user have no friends yet.'; +$string['ihavenofriends'] = 'No friends found. Use the button Add as friend on another user profile to add a friend.'; +$string['introductionmessage'] = 'Introduction message'; +$string['listoffriends'] = 'List of friends'; +$string['messagedescription'] = 'Send a message to your friend. This message will be visible by anyone on the site.'; +$string['messages'] = 'Messages'; +$string['messagesent'] = 'Message sent successfully'; +$string['nomessages'] = 'No messages found'; +$string['notyetfriend'] = 'This user is not your friend yet'; +$string['pendingrequest'] = 'You have sent a friendship request to this user. Please wait until it is confirmed.'; +$string['pleasewaitconfirmation'] = 'Please wait until the user confirms it.'; +$string['requestfriendship'] = 'Request friendship from $a'; +$string['return'] = 'Return to the user\'s profile'; +$string['sendmessage'] = 'Send a message'; +$string['sendmessagebutton'] = 'Send the message'; +$string['sendmessagetitle'] = 'Send a message to this friend'; +$string['timeago'] = '$a ago'; +$string['userwrote'] = '$a wrote:'; +$string['wantstobefriend'] = '$a wants to be part of your friend list.'; + +?>